package cn.cht.service.impl;


import cn.cht.pojo.Token;
import cn.cht.repository.TokenRepository;
import cn.cht.service.TokenService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;


import java.util.Optional;
import java.util.UUID;


@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    TokenRepository repository;

    @Override
    public String info(String tokenStr) {
        Token token = new Token();
        token.setToken(tokenStr);
        return repository.findOne(Example.of(token)).orElse(new Token()).getEmail();
    }

    @Override
    public Token save(String email) {
        Token token = new Token();
        token.setEmail(email);
        Optional<Token> one = repository.findOne(Example.of(token));
        if (one.isEmpty()) {
            token.setToken(UUID.randomUUID().toString());
            return repository.save(token);
        } else {
            return one.get();
        }
    }
}
